<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>

	</body>

	<script>
		class Iterator {
			constructor(container) {
				this.list = container.list
				this.index = 0
			}
			next() {
				if(this.hasNext()) {
					return this.list[this.index++]
				}
				return null
			}
			hasNext() {
				if(this.index >= this.list.length) {
					return false
				}
				return true
			}
		}

		class Container {
			constructor(list) {
				this.list = list
			}
			//生成遍历器
			getIterator() {
				return new Iterator(this)
			}
		}
		
		let arr = [1, 2, 3, 4, 5, 6]
		let container = new Container(arr)
		let iterator = container.getIterator()
		while(iterator.hasNext()) {
			console.log(iterator.next())//输出1到6
		}
		
	</script>

</html>